package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.User;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ReturnModel login(@RequestBody User user) {
        Boolean login = userService.login(user.getUsername(), user.getPassword());
        if (login) {
            return new ReturnModel(200, null, "登录成功");
        } else {
            return new ReturnModel(401, null, "用户名或密码错误");
        }
    }

    @GetMapping("/logout")
    public ReturnModel logout(HttpSession session) {
        session.invalidate(); // 清除会话
        return new ReturnModel(200, null, "登出成功");
    }

}
